1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package sun.font;
27
28 import java.lang.ref.Reference;
29 import java.awt.FontFormatException;
30 import java.awt.geom.GeneralPath;
31 import java.awt.geom.Point2D;
32 import java.awt.geom.Rectangle2D;
33 import java.io.File;
34 import java.nio.ByteBuffer;
35 import sun.java2d.Disposer;
36 import sun.java2d.DisposerRecord;
37
38 import java.io.IOException;
39 import java.security.AccessController;
40 import java.security.PrivilegedActionException;
41 import java.security.PrivilegedExceptionAction;
42
43 public abstract class FileFont extends PhysicalFont {
44
45 protected boolean useJavaRasterizer = true;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 protected int fileSize;
64
65 protected FontScaler scaler;
66
67
68
69
70
71
72
73
74
75
76
77
78 protected boolean checkedNatives;
79 protected boolean useNatives;
80 protected NativeFont[] nativeFonts;
81 protected char[] glyphToCharMap;
82
83
84
85 FileFont(String platname, Object nativeNames)
86 throws FontFormatException {
87
88 super(platname, nativeNames);
89 }
90
91 FontStrike createStrike(FontStrikeDesc desc) {
92 if (!checkedNatives) {
93 checkUseNatives();
94 }
95 return new FileFontStrike(this, desc);
96 }
97
98 protected boolean checkUseNatives() {
99 checkedNatives = true;
100 return useNatives;
101 }
102
103
104
105
106 protected abstract void close();
107
108
109
110
111
112
113 abstract ByteBuffer readBlock(int offset, int length);
114
115 public boolean canDoStyle(int style) {
116 return true;
117 }
118
119 void setFileToRemove(File file, CreatedFontTracker tracker) {
120 Disposer.addObjectRecord(this,
121 new CreatedFontFileDisposerRecord(file, tracker));
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 synchronized void deregisterFontAndClearStrikeCache() {
152 SunFontManager fm = SunFontManager.getInstance();
153 fm.deRegisterBadFont(this);
154
155 for (Reference strikeRef : strikeCache.values()) {
156 if (strikeRef != null) {
157
158
159
160 FileFontStrike strike = (FileFontStrike)strikeRef.get();
161 if (strike != null && strike.pScalerContext != 0L) {
162 scaler.invalidateScalerContext(strike.pScalerContext);
163 }
164 }
165 }
166 scaler.dispose();
167 scaler = FontScaler.getNullScaler();
168 }
169
170 StrikeMetrics getFontMetrics(long pScalerContext) {
171 try {
172 return getScaler().getFontMetrics(pScalerContext);
173 } catch (FontScalerException fe) {
174 scaler = FontScaler.getNullScaler();
175 return getFontMetrics(pScalerContext);
176 }
177 }
178
179 float getGlyphAdvance(long pScalerContext, int glyphCode) {
180 try {
181 return getScaler().getGlyphAdvance(pScalerContext, glyphCode);
182 } catch (FontScalerException fe) {
183 scaler = FontScaler.getNullScaler();
184 return getGlyphAdvance(pScalerContext, glyphCode);
185 }
186 }
187
188 void getGlyphMetrics(long pScalerContext, int glyphCode, Point2D.Float metrics) {
189 try {
190 getScaler().getGlyphMetrics(pScalerContext, glyphCode, metrics);
191 } catch (FontScalerException fe) {
192 scaler = FontScaler.getNullScaler();
193 getGlyphMetrics(pScalerContext, glyphCode, metrics);
194 }
195 }
196
197 long getGlyphImage(long pScalerContext, int glyphCode) {
198 try {
199 return getScaler().getGlyphImage(pScalerContext, glyphCode);
200 } catch (FontScalerException fe) {
201 scaler = FontScaler.getNullScaler();
202 return getGlyphImage(pScalerContext, glyphCode);
203 }
204 }
205
206 Rectangle2D.Float getGlyphOutlineBounds(long pScalerContext, int glyphCode) {
207 try {
208 return getScaler().getGlyphOutlineBounds(pScalerContext, glyphCode);
209 } catch (FontScalerException fe) {
210 scaler = FontScaler.getNullScaler();
211 return getGlyphOutlineBounds(pScalerContext, glyphCode);
212 }
213 }
214
215 GeneralPath getGlyphOutline(long pScalerContext, int glyphCode, float x, float y) {
216 try {
217 return getScaler().getGlyphOutline(pScalerContext, glyphCode, x, y);
218 } catch (FontScalerException fe) {
219 scaler = FontScaler.getNullScaler();
220 return getGlyphOutline(pScalerContext, glyphCode, x, y);
221 }
222 }
223
224 GeneralPath getGlyphVectorOutline(long pScalerContext, int[] glyphs, int numGlyphs, float x, float y) {
225 try {
226 return getScaler().getGlyphVectorOutline(pScalerContext, glyphs, numGlyphs, x, y);
227 } catch (FontScalerException fe) {
228 scaler = FontScaler.getNullScaler();
229 return getGlyphVectorOutline(pScalerContext, glyphs, numGlyphs, x, y);
230 }
231 }
232
233
234
235 protected abstract FontScaler getScaler();
236
237 protected long getUnitsPerEm() {
238 return getScaler().getUnitsPerEm();
239 }
240
241 private static class CreatedFontFileDisposerRecord
242 implements DisposerRecord {
243
244 File fontFile = null;
245 CreatedFontTracker tracker;
246
247 private CreatedFontFileDisposerRecord(File file,
248 CreatedFontTracker tracker) {
249 fontFile = file;
250 this.tracker = tracker;
251 }
252
253 public void dispose() {
254 java.security.AccessController.doPrivileged(
255 new java.security.PrivilegedAction() {
256 public Object run() {
257 if (fontFile != null) {
258 try {
259 if (tracker != null) {
260 tracker.subBytes((int)fontFile.length());
261 }
262
263
264
265
266
267
268 fontFile.delete();
269
270
271 SunFontManager.getInstance().tmpFontFiles.remove(fontFile);
272 } catch (Exception e) {
273 }
274 }
275 return null;
276 }
277 });
278 }
279 }
280
281 protected String getPublicFileName() {
282 SecurityManager sm = System.getSecurityManager();
283 if (sm == null) {
284 return platName;
285 }
286 boolean canReadProperty = true;
287
288 try {
289 sm.checkPropertyAccess("java.io.tmpdir");
290 } catch (SecurityException e) {
291 canReadProperty = false;
292 }
293
294 if (canReadProperty) {
295 return platName;
296 }
297
298 final File f = new File(platName);
299
300 Boolean isTmpFile = Boolean.FALSE;
301 try {
302 isTmpFile = AccessController.doPrivileged(
303 new PrivilegedExceptionAction<Boolean>() {
304 public Boolean run() {
305 File tmp = new File(System.getProperty("java.io.tmpdir"));
306 try {
307 String tpath = tmp.getCanonicalPath();
308 String fpath = f.getCanonicalPath();
309
310 return (fpath == null) || fpath.startsWith(tpath);
311 } catch (IOException e) {
312 return Boolean.TRUE;
313 }
314 }
315 }
316 );
317 } catch (PrivilegedActionException e) {
318
319
320 isTmpFile = Boolean.TRUE;
321 }
322
323 return isTmpFile ? "temp file" : platName;
324 }
325 }